package com.lanmon.controller.auth;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanmon.base.ComConstants;
import com.lanmon.base.PageEntity;
import com.lanmon.base.RequestResult;
import com.lanmon.entity.ComUser;
import com.lanmon.service.auth.ComUserService;
import com.lanmon.service.auth.OrganizationService;
import com.lanmon.util.Coder;
import com.lanmon.util.CommonUtil;
import com.lanmon.util.DateUtil;

/**
 * 系统用户信息维护页面控制器
 * 
 * @author resigshy
 * 
 */
@Controller
@RequestMapping("/auth")
public class UserMaintainController {
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// 注册自定义的属性编辑器
		CustomDateEditor dateEditor = new CustomDateEditor(DateUtil.DEFAULT_SDF, true);
		// 表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换
		binder.registerCustomEditor(Date.class, dateEditor);
	}
	
	@Autowired
	private ComUserService comUserService;
	
	@Autowired
	private OrganizationService organizationService;
	
	@RequestMapping("/userMaintain")
	public String init() {
		return "/auth/userMaintain";
	}
	
	@RequestMapping("/allusers")
	@ResponseBody
	public Map<String, Object> getAllUsers(PageEntity<ComUser> entity, ComUser user) {
		Map<String, Object> map = new HashMap<>();
		try {
			comUserService.findFetchPage(entity, user);
			map.put("users", entity.getResult());
			map.put("totalPageCount", entity.getTotalPageCount());
			return map;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	public RequestResult addUser(ComUser user) {
		if (user == null) {
			return new RequestResult(false, "", "用户信息保存失败！");
		}
		List<ComUser> userList = comUserService.findObjects(Restrictions.eq("username", user.getUsername()));
		try {
			if (user.getUserid() != null) {
				ComUser userExists = comUserService.getObject(user.getUserid());
				if (userList.size() == 1 && userList.get(0).getUserid() != userExists.getUserid()) {
					return new RequestResult(false, "", "用户名" + user.getUsername() + "重复！");
				}
				CommonUtil.populate(user, userExists);
				if (StringUtils.isEmpty(userExists.getPassword()) || ComConstants.USER_DEFAULT_PWD.equals(userExists.getPassword())) {
					userExists.setPassword(Coder.md5Pass(ComConstants.USER_DEFAULT_PWD));
				}
				comUserService.saveOrUpdateObject(userExists);
			}
			else {
				if (userList.size() > 0) {
					return new RequestResult(false, "", "用户名" + user.getUsername() + "重复！");
				}
				user.setPassword(Coder.md5Pass(ComConstants.USER_DEFAULT_PWD));
				comUserService.saveObject(user);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return new RequestResult(true, "", "用户信息保存成功！");
	}
	
	@RequestMapping("/deluser")
	@ResponseBody
	public RequestResult delUser(String userid) {
		RequestResult rr = new RequestResult();
		if (!ComConstants.EMPTY_STR.equals(userid)) {
			comUserService.destroyAllObject(userid);
			rr.setMessage("用户信息删除成功！");
		}
		else {
			rr.setMessage("用户信息删除失败！");
		}
		return rr;
	}
}
